/**
 * @FileDescription 战时标绘 创建地图要素
 * @Author 祝小文
 * @Date 2022-11-22 13:35
 */

import { requireImage } from '@/utils'

/** 通过数据加载全部地图要素 */
export function loadWarAllMapFactor(data: any) {
  const firefightPlotStore = useFirefightPlotStore()

  data.forEach((item: any) => {
    //如果位置信息是字符串的经纬度 需要转成笛卡尔对象
    if (typeof item.wzxx === 'string') {
      item.wzxx = JSON.parse(item.wzxx)
      if (item.wzxx instanceof Array) {
        item.wzxx = item.wzxx.map((item: any) => {
          return window.cesiumMapTool.WGS84ToCar3(item)
        })
      } else if (item.wzxx instanceof Object) {
        item.wzxx = window.cesiumMapTool.WGS84ToCar3(item.wzxx)
      }
    }

    if (item.zxjd && item.zxwd) {
      item.zxdjdwd = window.cesiumMapTool.WGS84ToCar3({
        lng: item.zxjd,
        lat: item.zxwd,
        alt: item.zxgd,
      })
    }

    //---------- 1. 圆 ----------
    if (firefightPlotStore.bzlxWarCircle.includes(item.bzlx)) {
      //圆
      if (item.bzlx === '0') {
        createWarCircle({
          name: item.id,
          parentName: item.cjid,
          position: item.wzxx,
          labelText: item.zj,
          circleFillColor: 'rgba(255, 0, 0, 0.5)',
          radius: item.bj,
          attribute: { bzlx: item.bzlx, zj: item.zj },
        })
      }
      //圆（线）
      else if (item.bzlx === '1') {
        createWarCircle({
          name: item.id,
          parentName: item.cjid,
          position: item.wzxx,
          labelText: item.zj,
          radius: item.bj,
          attribute: { bzlx: item.bzlx, zj: item.zj },
        })
      }
    }

    //---------- 2. 文字 ----------
    else if (firefightPlotStore.bzlxWarStickLabel.includes(item.bzlx)) {
      createWarStickLabel({
        name: item.id,
        parentName: item.cjid,
        position: item.wzxx,
        labelText: item.zj,
        rotation: item.xzjd,
        attribute: { bzlx: item.bzlx, zj: item.zj },
      })
    }

    //---------- 3. 标牌 ----------
    else if (firefightPlotStore.bzlxWarSign.includes(item.bzlx)) {
      const option: any = {
        name: item.id,
        parentName: item.cjid,
        position: item.wzxx,
        labelText: item.bzmc,
        rotation: item.xzjd,
        attribute: { bzlx: item.bzlx, zj: item.zj, bzmc: item.bzmc },
      }
      switch (item.bzlx) {
        //指挥部
        case '7':
          option.imgUrl = requireImage('feature/command.png')
          break
        //消防水枪
        case '13':
          option.imgUrl = requireImage('feature/fireHoses.png')
          break
        //泵组供水点
        case '14':
          option.imgUrl = requireImage('feature/fireMonitor.png')
          break
        //消防炮
        case '15':
          option.imgUrl = requireImage('feature/pumpPoint.png')
          break
        //事故点
        case '19':
          option.imgUrl = requireImage('feature/accidentPoint.png')
          break
        //禁止通行
        case '20':
          option.imgUrl = requireImage('feature/noThoroughfare.png')
          break
        //被困人员
        case '21':
          option.imgUrl = requireImage('feature/trappedPerson.png')
          break
      }
      createWarSign(option)
    }

    //---------- 4. 消防车标牌 ----------
    else if (firefightPlotStore.bzlxWarCar.includes(item.bzlx)) {
      const option: any = {
        name: item.id,
        parentName: item.cjid,
        position: item.wzxx,
        labelTopText: item.zjmc || item.zj,
        labelBottomText: item.bzmc,
        rotation: item.xzjd,
        attribute: { bzlx: item.bzlx, zj: item.zj, bzmc: item.bzmc },
      }
      switch (item.bzlx) {
        //举高类消防车
        case '8':
          option.imgUrl = requireImage('feature/map/rearCar.png')
          break
        //灭火类消防车
        case '9':
          option.imgUrl = requireImage('feature/map/firefightCar.png')
          break
        //抢险类消防车
        case '10':
          option.imgUrl = requireImage('feature/map/firefightCar.png')
          break
        //专勤类消防车
        case '11':
          option.imgUrl = requireImage('feature/map/firefightCar.png')
          break
        //特种装备
        case '12':
          option.imgUrl = requireImage('feature/map/specialDevice.png')
          break
      }
      createWarCar(option)
    }

    //---------- 5. 线 ----------
    else if (firefightPlotStore.bzlxWarPolyline.includes(item.bzlx)) {
      const option: any = {
        name: item.id,
        parentName: item.cjid,
        position: item.wzxx,
        isArrow: true,
        fillColor: 'red',
        width: 20,
        attribute: { bzlx: item.bzlx, zj: item.zj },
      }
      switch (item.bzlx) {
        //实线箭头
        case '4':
          option.labelText = item.zj
          break
        //线
        case '5':
          option.isArrow = false
          option.width = 10
          option.labelText = item.zj
          break
        //进攻路线
        case '16':
          // option.clampToGround = true
          option.labelText = '进攻路线'
          break
        //撤离路线
        case '17':
          // option.clampToGround = true
          option.fillColor = 'yellow'
          option.labelText = '撤离路线'
          break
        //进攻方向
        case '18':
          option.labelText = '进攻方向'
          break
      }
      createWarPolyline(option)
    }

    //---------- 6. 多边形 ----------
    else if (firefightPlotStore.bzlxWarPolygon.includes(item.bzlx)) {
      const option: any = {
        name: item.id,
        parentName: item.cjid,
        position: item.wzxx,
        zxdjdwd: item.zxdjdwd,
        labelText: item.zj,
        attribute: { bzlx: item.bzlx, zj: item.zj },
      }
      switch (item.bzlx) {
        //多边形(面)
        case '2':
          option.fillColor = 'rgba(255, 0, 0, 0.5)'
          break
        //多边形(线)
        case '3':
          break
        //事故区
        case '22':
          option.labelBckColor = 'yellow'
          break
        //集结区
        case '23':
          option.labelBckColor = 'yellow'
          option.fillColor = 'rgba(255, 0, 0, 0.5)'
          break
        //待命区
        case '24':
          option.fillColor = 'rgba(255, 255, 0, 0.5)'
          option.polylineColor = 'yellow'
          option.polylineType = 'dashed'
          break
        //禁停区
        case '25':
          option.polylineColor = 'yellow'
          option.material = new Cesium.StripeMaterialProperty({
            evenColor: Cesium.Color.YELLOW.withAlpha(0.2),
            oddColor: Cesium.Color.YELLOW.withAlpha(1),
            repeat: 60,
          })
          break
        //装备器材区
        case '26':
          option.labelBckColor = 'yellow'
          option.fillColor = 'rgba(255, 255, 0, 0.5)'
          option.polylineColor = 'yellow'
          break
        //警戒区
        case '27':
          option.polylineColor = 'yellow'
          break
      }
      createWarPolygon(option)
    }
  })
}

/** 创建圆 */
export function createWarCircle(option: any) {
  window.GWarCircle.add({
    name: option.name,
    parentName: option.parentName,
    option: {
      position: option.position,
      label: {
        text: option.labelText,
      },
      circle: {
        radius: option.radius || 5,
        fillColor: option.circleFillColor,
      },
      attribute: option.attribute,
    },
  })
}

/** 创建文字 */
export function createWarStickLabel(option: any) {
  window.GWarStickLabel.add({
    name: option.name,
    parentName: option.parentName,
    option: {
      position: option.position,
      label: {
        text: option.labelText,
        fontSize: 150,
        color: option.color || '#FFFF00',
      },
      scale: 1,
      rotation: option.rotation || 0,
      attribute: option.attribute,
    },
  })
}

/** 创建标牌 */
export function createWarSign(option: any) {
  window.GWarSign.add({
    name: option.name,
    parentName: option.parentName,
    option: {
      position: option.position,
      image: {
        url: option.imgUrl,
      },
      label: {
        text: option.labelText,
        fontSize: 50,
        color: option.labelColor || '#FFFF00',
      },
      scale: 1,
      rotation: option.rotation || 0,
      attribute: option.attribute,
    },
  })
}

/** 创建消防车标牌 */
export function createWarCar(option: any) {
  window.GWarCar.add({
    name: option.name,
    parentName: option.parentName,
    option: {
      position: option.position,
      image: {
        url: option.imgUrl,
      },
      labelTop: {
        text: option.labelTopText || '',
        fontSize: 50,
        color: option.labelTopColor || '#FFFF00',
      },
      labelBottom: {
        text: option.labelBottomText || '',
        fontSize: 50,
        color: option.labelBottomColor || '#FFFFFF',
      },
      scale: 1,
      rotation: option.rotation || 0,
      attribute: option.attribute,
    },
  })
}

/** 创建多边形 */
export function createWarPolygon(option: any) {
  window.GWarPolygon.add({
    name: option.name,
    parentName: option.parentName,
    option: {
      position: option.position,
      centerpos: option.zxdjdwd,
      label: {
        text: option.labelText,
        fillColor: option.labelBckColor,
      },
      polygon: {
        fillColor: option.fillColor,
      },
      polyline: {
        color: option.polylineColor,
        type: option.polylineType,
        dashLength: option.polylineDashLength,
      },
      material: option.material,
      attribute: option.attribute,
    },
  })
}

/** 创建线 */
export function createWarPolyline(option: any) {
  window.GWarPolyline.add({
    name: option.name,
    parentName: option.parentName,
    option: {
      label: {
        text: option.labelText,
        fontSize: 150,
        color: option.color || '#FFFF00',
      },
      scale: 1,
      position: option.position,
      isArrow: option.isArrow,
      fillColor: option.fillColor,
      width: option.width,
      clampToGround: option.clampToGround,
      attribute: option.attribute,
    },
  })
}
